﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CommonHelp
{
    public class LogHelper
    {
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg)
        {
            try
            {
                msg = "\r\n" + System.DateTime.Now.ToString() + " : " + msg;
                string path = Application.StartupPath + "//Log//Error" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

                FileStream fileStream = null;
                if (!File.Exists(path))
                {
                    Directory.CreateDirectory(Application.StartupPath + "//Log");
                    fileStream = File.Create(path);
                }
                if (fileStream == null)
                {
                    fileStream = new FileStream(path, FileMode.Append);
                }
                byte[] info = new System.Text.UTF8Encoding(true).GetBytes(msg);
                fileStream.Write(info, 0, info.Length);
                fileStream.Close(); 
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 记录详细的错误信息
        /// </summary>
        /// <param name="ex"></param>
        public static void Error(Exception ex)
        {
            string msg = GetErrorInfo(ex, null);
            Error(msg);
        }

        public static string GetErrorInfo(Exception ex, String info)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                sb.Append(Environment.NewLine + "**************************************************************");
                if (!String.IsNullOrEmpty(info))
                {
                    sb.AppendLine(info);
                }
                sb.Append(Environment.NewLine + "基本信息:");
                sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                sb.Append(Environment.NewLine + "");
                if (ex != null)
                {
                    sb.Append(Environment.NewLine + "-------------------------------------------");
                    sb.Append(Environment.NewLine + "错误信息：" + ex.Message);
                    sb.Append(Environment.NewLine + "类名：" + ex.TargetSite.DeclaringType.FullName);
                    sb.Append(Environment.NewLine + "方法名：" + ex.TargetSite.Name);
                    sb.Append(Environment.NewLine + "堆栈：<pre>" + ex.StackTrace + "</pre>");
                    sb.Append(Environment.NewLine + "");
                    sb.Append(Environment.NewLine + "-------------------------------------------");
                }
                sb.Append(Environment.NewLine + "**************************************************************");
            }
            catch (Exception exc)
            {
                Error("获取错误信息时出现异常" + exc.Message);
            }
            return sb.ToString();
        }
    }
}
